#include "StdAfx.h"
#include "SingleLinePickUI.h"

CSingleLinePickUI::CSingleLinePickUI(void): m_cxWidth(0), m_nLinks(0), m_uButtonState(0)
{
	::ZeroMemory(&m_rcLinks, sizeof(m_rcLinks));
	::ZeroMemory(&m_rcButton, sizeof(m_rcButton));
}

CSingleLinePickUI::~CSingleLinePickUI(void)
{
}

LPCTSTR CSingleLinePickUI::GetClass() const
{
	return _T("SinglePrettyEditUI");
}

UINT CSingleLinePickUI::GetControlFlags() const
{
	return UIFLAG_SETCURSOR | UIFLAG_TABSTOP;
}

void CSingleLinePickUI::Event(TEventUI& event)
{
	if( event.Type == UIEVENT_SETCURSOR )
	{
		for( int i = 0; i < m_nLinks; i++ ) {
			if( ::PtInRect(&m_rcLinks[i], event.ptMouse) ) {
				::SetCursor(::LoadCursor(NULL, MAKEINTRESOURCE(IDC_HAND)));
				return;
			}
		}      
	}
	if( event.Type == UIEVENT_BUTTONDOWN && IsEnabled() )
	{
		if( ::PtInRect(&m_rcButton, event.ptMouse) ) {
			m_uButtonState |= UISTATE_PUSHED | UISTATE_CAPTURED;
			Invalidate();
		}
		else {
			// Check for link press
			for( int i = 0; i < m_nLinks; i++ ) {
				if( ::PtInRect(&m_rcLinks[i], event.ptMouse) ) {
					m_pManager->SendNotify(this, _T("link"));
					return;
				}
			}      
		}
		return;
	}
	if( event.Type == UIEVENT_MOUSEMOVE )
	{
		if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
			if( ::PtInRect(&m_rcButton, event.ptMouse) ) m_uButtonState |= UISTATE_PUSHED;
			else m_uButtonState &= ~UISTATE_PUSHED;
			Invalidate();
		}
	}
	if( event.Type == UIEVENT_BUTTONUP )
	{
		if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
			if( ::PtInRect(&m_rcButton, event.ptMouse) ) m_pManager->SendNotify(this, _T("browse"));
			m_uButtonState &= ~(UISTATE_PUSHED | UISTATE_CAPTURED);
			Invalidate();
		}
	}
	if( event.Type == UIEVENT_KEYDOWN ) 
	{
		if( event.chKey == VK_SPACE && m_nLinks > 0 ) m_pManager->SendNotify(this, _T("link"));
		if( event.chKey == VK_F4 && IsEnabled() ) m_pManager->SendNotify(this, _T("browse"));
	}
	CControlUI::Event(event);
}

void CSingleLinePickUI::SetWidth(int cxWidth)
{
	m_cxWidth = cxWidth;
	UpdateLayout();
}

SIZE CSingleLinePickUI::EstimateSize(SIZE /*szAvailable*/)
{
	SIZE sz = { 0, 12 + m_pManager->GetThemeFontInfo(UIFONT_NORMAL).tmHeight };
	if( m_cxWidth > 0 ) {
		sz.cx = m_cxWidth;
		RECT rcText = m_rcItem;
		::InflateRect(&rcText, -4, -2);
		m_nLinks = lengthof(m_rcLinks);
		CBlueRenderEngineUI::DoPaintPrettyText(m_pManager->GetPaintDC(), m_pManager, rcText, m_sText, 
			UICOLOR_EDIT_TEXT_NORMAL, UICOLOR__INVALID, m_rcLinks, m_nLinks, DT_SINGLELINE | DT_CALCRECT);
		sz.cy = rcText.bottom - rcText.top;
	}
	return sz;
}

void CSingleLinePickUI::DoPaint(HDC hDC, const RECT& rcPaint)
{
	int cy = m_rcItem.bottom - m_rcItem.top;
	::SetRect(&m_rcButton, m_rcItem.right - cy, m_rcItem.top, m_rcItem.right, m_rcItem.bottom);
	RECT rcText = { m_rcItem.left, m_rcItem.top, m_rcButton.left - 4, m_rcItem.bottom };
	UITYPE_COLOR iTextColor = UICOLOR_EDIT_TEXT_NORMAL;
	UITYPE_COLOR iBorderColor = UICOLOR_CONTROL_BORDER_NORMAL;
	UITYPE_COLOR iBackColor = UICOLOR_CONTROL_BACKGROUND_NORMAL;
	if( IsFocused() ) {
		iTextColor = UICOLOR_EDIT_TEXT_NORMAL;
		iBorderColor = UICOLOR_CONTROL_BORDER_NORMAL;
		iBackColor = UICOLOR_CONTROL_BACKGROUND_HOVER;
	}
	if( !IsEnabled() ) {
		iTextColor = UICOLOR_EDIT_TEXT_DISABLED;
		iBorderColor = UICOLOR_CONTROL_BORDER_DISABLED;
		iBackColor = UICOLOR__INVALID;
	}
	CBlueRenderEngineUI::DoPaintFrame(hDC, m_pManager, rcText, iBorderColor, iBorderColor, iBackColor);
	::InflateRect(&rcText, -4, -2);
	m_nLinks = lengthof(m_rcLinks);
	CBlueRenderEngineUI::DoPaintPrettyText(hDC, m_pManager, rcText, m_sText, iTextColor, UICOLOR__INVALID, 
		m_rcLinks, m_nLinks, DT_SINGLELINE);
	RECT rcPadding = { 0 };
	CBlueRenderEngineUI::DoPaintButton(hDC, m_pManager, m_rcButton, _T("<i 4>"), rcPadding, m_uButtonState, 0);
}
